package com.gopay.bis.ipay.ebank.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import net.sf.json.JSONNull;
import net.sf.json.JSONObject;


public class PlainTextUtil {

	public static String getPlainText(JSONObject jObj) {
		StringBuffer content = new StringBuffer();
		Set<String> sets = jObj.keySet();
		List<String> keys = new ArrayList<String>(sets);
		Collections.sort(keys, String.CASE_INSENSITIVE_ORDER);
		for (int i = 0; i < keys.size(); i++) {
			String key = (String) keys.get(i);
			if ("sign".equals(key)) {
				continue;
			}
			if(jObj.get(key)==null || jObj.get(key).equals("")|| jObj.get(key).toString().equals("null")){	
			    continue;
			}
			
			String value = (String) jObj.get(key);
			if (isnull(value)) {
				continue;	
			}
			content.append((i == 0 ? "" : "&") + key + "=" + value);

		}
		String signSrc = content.toString();
		if (signSrc.startsWith("&")) {
			signSrc = signSrc.replaceFirst("&", "");
		}
		return signSrc;
	}

	public static boolean isnull(String str) {
		if (null == str || str.equalsIgnoreCase("null") || str.trim().equals("")) {
			return true;
		} else
			return false;
	}

	public static void main(String[] args) {
		String x = "{	'oid_partner':'201103171000000000',	'dt_order':null,	'no_order':'2013051500001','result_pay':'SUCCESS','oid_paybill':'2013051500001','money_order':'49.65','settle_date':'20130515','info_order':'用户13958069593购买了3桶羽毛球','pay_type':'2',	'bank_code':'01020000','sign':'ZPZULntRpJwFmGNIVKwjLEF2Tze7bqs60rxQ22CqT5J1UlvGo575QK9z/+p+7E9cOoRoWzqR6xHZ6WVv3dloyGKDR0btvrdqPgUAoeaX/YOWzTh00vwcQ+HBtXE+vPTfAqjCTxiiSJEOY7ATCF1q7iP3sfQxhS0nDUug1LP3OLk='}";
		JSONObject reqObj = JSONObject.fromObject(x);
		System.out.println(getPlainText(reqObj));
	}
}
